// pages/doctor-my-case/doctor-my-case.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,  
    localImg: app.globalData.staticimgUrl,       // 本地图片
    content: "",
    replyId: "",
    currentTab: 0,
    firsttype: 1, // 一级分类
    secondtype: 1, // 二级分类
    thirdtype: 1, // 三级分类
    choosed: [1, 2, 3],
    pageNum: 1,
    pageSize: 5,
    isshowadd: 0, // 控制是否显示添加按钮
    typelist: [],
    caseinfo: [],
    more: 0, // 是否为上拉加载
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var isshowadd = options.isShowAdd;
    this.setData({
      'isshowadd': isshowadd
    });
  },

  // 一级服务产品类别列表
  oneTypeServiceList: function (e) {
    var that = this;
    app.request({
      url: 'shopp/goodsTypeApi/getPage',
      data: {
        'type': 2,
      },
      success: function (res) {
        that.setData({
          'typelist': res.data
        });
        // 获取案例
        that.getCase();
      }
    })
  },

  // 获取我的案例
  getCase: function(e) {
    // 医生端获取医生id 用户端不传
    var that = this;
    let { typelist, currentTab } = this.data;
    let sort = typelist[currentTab]['id'];
    var doctorId = wx.getStorageSync('userid');
    app.request({
      url: 'doctor/docExample/getExampleList',
      data: {
        doctorId: doctorId,
        oneTypeId: sort,
        pageNum: that.data.pageNum,
        pageSize: that.data.pageSize,
      },
      success(res) {

        wx.hideLoading();
        if (res.data) {
          if (that.data.more == 0) {
            that.setData({
              'caseinfo': res.data,
            })
          } else {
            that.setData({
              more: 0,
            })
            if (res.data.length) {
              var info = that.data.caseinfo.concat(res.data);
              that.setData({
                'caseinfo': info,
              })
            } else {
              wx.showToast({
                title: '已无更多...',
                icon: 'none',
                duration: 1000,
              });
            }
          }
        }
      }
    })
  
  },
  // 导航切换
  onChangeTab: function(e){

    this.setData({
      currentTab: e.detail.index,
      pageNum: 1
    });
    this.getCase();
  },
  // 修改案例
  modifyCase(e){
    wx.navigateTo({
      url: "/pages/doctor-case-add-4/doctor-case-add-4?exampleId=" + e.currentTarget.dataset.id
    });
  },

  // 删除案例
  deleteCase: function(e) {
    const that = this;
    const param = {
      exampleId: e.currentTarget.dataset.id,
      state: 4
    };
    app.request({
      url: "doctor/docExample/updateState",
      data: param,
      success: function (res) {
        res.status_code == 200 && app.showToast("删除成功")
        that.oneTypeServiceList(); 
      },
      fail: function (res) {
        // app.requestFail(res);
      }
    });
  },

  // 关注按钮
  bindFollow: function(e) {
    var doctorid = e.currentTarget.dataset.doctorid;
    var that = this;
    app.request({
      url: 'doctor/member/attention',
      data: {
        userId: doctorid,
      },
      success(res) {
       
        that.setData({
          pageNum: 1,
        })
        that.getCase();
        wx.showToast({
          title: '关注成功',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      'more': 0,
      'pageNum': 1,
    })
    this.oneTypeServiceList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    wx.showLoading({
      title: '加载中...',
    })
    this.setData({
      'more': 1,
      'pageNum': this.data.pageNum + 1,
    });
 
    this.getCase();
  }
})